package entity

import (
	"encoding/json"
	"time"
)

type RolePay struct {
	RoleID      int32 `gorm:"type:int;primary_key"`
	Diamond     int32
	GetFirstPay bool
	VIPRecord   []byte `gorm:"column:vip_record;type:jsonb"`
	CreatedAt   time.Time
	UpdatedAt   time.Time

	vipRecord map[int32]bool `sql:"-"` // vip 奖励领取记录
}

func (e *RolePay) AfterFind() error {

	e.vipRecord = make(map[int32]bool)
	_ = json.Unmarshal(e.VIPRecord, &e.vipRecord)
	return nil
}

func (e *RolePay) BeforeSave() error {

	if e.vipRecord == nil {
		e.vipRecord = make(map[int32]bool)
	}

	e.VIPRecord, _ = json.Marshal(e.vipRecord)
	return nil
}

func (e *RolePay) BeforeUpdate() error {

	if e.vipRecord == nil {
		e.vipRecord = make(map[int32]bool)
	}

	e.VIPRecord, _ = json.Marshal(e.vipRecord)
	return nil
}

func (e *RolePay) GetVIPRecord() map[int32]bool {

	return e.vipRecord
}

func (e *RolePay) SetVIPRecord(VIPLv int32) {

	e.vipRecord[VIPLv] = true
}
